package com.kangfu.api.service.impl;

import java.util.List;


import com.kangfu.api.service.IArticleService;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.db.mapper.ArticleMapper;
import com.kangfu.db.mapper.LessonMapper;
import com.kangfu.db.model.vo.ArticleInfoVo;
import com.kangfu.db.model.vo.ArticleVo;
import com.kangfu.db.model.vo.MainArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 文章Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-20
 */
@Service
public class ArticleServiceImpl implements IArticleService
{
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private LessonMapper lessonMapper;

    /**
     * 查询文章
     * 
     * @param id 文章主键
     * @return 文章
     */
    @Override
    public ArticleInfoVo selectArticleById(Integer userId, Integer id)
    {
        ArticleInfoVo articleInfoVo = articleMapper.selectArticleById(id);
        //检查用户是否可以解锁这篇资讯
        //获取用户当前打卡次数
        Integer checkinCount = lessonMapper.getCheckinCountByUserId(userId);
        if (checkinCount == null) {
            checkinCount = 0;
        }
        System.out.println(checkinCount);
        if (checkinCount < articleInfoVo.getUnlockCheckinCount()) {
            throw new BusinessException("需要" + articleInfoVo.getUnlockCheckinCount() + "次运动打卡方可解锁该篇资讯");
        }
        //阅读数加 1
        articleMapper.addReadCount(id);

        return articleInfoVo;
    }

    /**
     * 查询文章列表
     * 
     * @param page 页码
     * @return 文章
     */
    @Override
    public List<ArticleVo> selectArticleList(Integer userId, int page)
    {
        int pageSize = 5;
        int offset = (page-1)*pageSize;
        List<ArticleVo> list = articleMapper.selectArticleList(offset, pageSize);
        //获取用户当前打卡次数
        Integer checkinCount = lessonMapper.getCheckinCountByUserId(userId);
        if (checkinCount == null) {
            checkinCount = 0;
        }
        System.out.println(checkinCount);
        for (ArticleVo articleVo : list) {
            Integer unlockCheckinCount = articleVo.getUnlockCheckinCount();
            if (unlockCheckinCount <= checkinCount) {
                articleVo.setUnlock(1);
                articleVo.setNeedCheckinCount(0);
            } else {
                articleVo.setUnlock(0);
                articleVo.setNeedCheckinCount(unlockCheckinCount - checkinCount);
            }
        }
        return list;

//        String tagIdStr = list.stream().map(e -> e.getTagId()).collect(Collectors.joining(","));
//        String[] tagIds = tagIdStr.split(",");
//        String distinctTagIds = Arrays.stream(tagIds).distinct().collect(Collectors.joining(","));
//
//        List<TagNameBo> tagNameBos = tagMapper.selectTagListByTagIds(distinctTagIds);
//        Map<Integer, String> tagNameMap = tagNameBos.stream().collect(Collectors.toMap(TagNameBo::getId, x -> x.getName()));
//
//
//        List<ArticleListVo> articleListVos = new ArrayList<ArticleListVo>();
//        for (Article article : list) {
//            ArticleListVo articleListVo = new ArticleListVo();
//            articleListVo.setId(article.getId());
//            articleListVo.setAuthor(article.getNickname());
//            articleListVo.setCover(article.getCover());
//            articleListVo.setCreateTime(article.getCreateTime());
//            articleListVo.setTitle(article.getTitle());
//            //获取tagnameList
//        }

    }

    /**
     * 查询首页的文章列表
     * @return
     */
    @Override
    public List<MainArticleVo> selectMainArticle(Integer userId) {
        List<MainArticleVo> list = articleMapper.selectMainArticle();
        //获取用户当前打卡次数
        Integer checkinCount = lessonMapper.getCheckinCountByUserId(userId);
        if (checkinCount == null) {
            checkinCount = 0;
        }
        System.out.println(checkinCount);
        for (MainArticleVo mainArticleVo : list) {
            Integer unlockCheckinCount = mainArticleVo.getUnlockCheckinCount();
            if (unlockCheckinCount <= checkinCount) {
                mainArticleVo.setUnlock(1);
                mainArticleVo.setNeedCheckinCount(0);
            } else {
                mainArticleVo.setUnlock(0);
                mainArticleVo.setNeedCheckinCount(unlockCheckinCount - checkinCount);
            }
        }
        return list;
    }

}
